import axios from 'axios'
import {
	Message,
	Loading
} from 'element-ui'
import qs from 'qs'
let loadingInstance = null // loading
axios.defaults.adapter = require('axios/lib/adapters/http');
axios.defaults.baseURL = `https://modao.cc/event_track`
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

// 请求拦截
axios.interceptors.request.use(
	config => {
		return config;
	},
	error => {
		console.log(error,'==error -- interceptors.request');
		return Promise.reject(error);
	}
);

// 响应拦截
axios.interceptors.response.use(
	response => {
		loadingInstance.close()
		return response;
	},
	error => {
		loadingInstance.close()
		return Promise.reject(error);
	}
);

export function get(url, params) {
	loadingInstance = Loading.service({
		lock: true,
		text: 'loading...'
	})
	return new Promise((resolve, reject) => {
		axios.get(url, {
			params: params
		}).then(res => {
			resolve(res.data);
		}).catch(err => {
			reject(err)
		})
	});
	
}


export function post(url, params) {
	loadingInstance = Loading.service({
		lock: true,
		text: 'loading...'
	})
	return new Promise((resolve, reject) => {
		axios.post(url, qs.stringify(params))
			.then(res => {
				resolve(res.data);
			})
			.catch(err => {
				reject(err)
			})
	});
}
